var app = require('express');
var router = app.Router();

const ROOT_PATH = require('app-root-path'),//项目根路径
  db = require(ROOT_PATH + "/db"),//项目根路径
  common = require(ROOT_PATH + "/common/common");

router.get('/test',function(req, res, next){
  res.json({
    msg: "访问成功",
    status: 200,
    data: [1, 2, 3]
  });
});
router.get('/querys',function(req, res, next){
  var reqData = common.is_key(req.query, req.body);
  let sql = "select * from person order by id desc limit ?,?;";
  let sql2 = "select * from person where id=?;";

  db.queryAlls(sql + sql2, 0, 3, 6)
    .then((data) => {
      res.json({
        data
      });
    });
});

module.exports = router;

/**
module.exports = {
  test(req, res, next){
    res.json({
        msg: "访问成功",
        status: 200,
        data: [1, 2, 3]
      })
  },
  hello(req, res, next) {
    var reqData = common.is_key(req.query, req.body);

    let page = reqData.page * 1 || 1;
    let limit = reqData.limit * 1 || 10;
    let sql = "select * from person order by id desc limit ?,?;";
    db.queryAll(sql, (page - 1) * limit, limit)
      .then((data) => {
        res.json({
          data
        });
      });
  },

  query(req, res, next) {
    var reqData = common.is_key(req.query, req.body);

    let sql = "select * from person where id=5;";
    db.queryOne(sql)
      .then((data) => {
        res.json({
          data
        });
      });
  },

  querys(req, res, next) {
    var reqData = common.is_key(req.query, req.body);

    let sql = "select * from person order by id desc limit ?,?;";
    let sql2 = "select * from person where id=?;";

    db.queryAlls(sql + sql2, 0, 3, 6)
      .then((data) => {
        res.json({
          data
        });
      });
  },

  goods(req, res, next) {
    var reqData = common.is_key(req.query, req.body);

    var page = (reqData.page) * 1 || 1;
    var limit = (reqData.limit) * 1 || 100;
    var sql = "select * from sjs_user limit ?;";
    var post = [(page - 1) * limit, limit];

    db.queryAll(sql, post).then(function (data) {
      return data;
    }).then(function (data_a) {
      db.querySingle("select count(*) count from sjs_user;").then(function (value) {
        let data = {
          "count": value,
          "list": data_a,
        };
        return common.resFun(data);
      }).then(function (data) {
        res.json(data);
      });
    })
  },
};
**/
